import 'package:flutter/cupertino.dart';
import 'package:sil_utils_package/sil_utils_package.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../base/SilScaffold.dart';
import '../../../utils/color/ColorUtils.dart';
import '../../widgets/uploadPics/UpLoadPicsWidget.dart';
import 'IIdeaFeedbackUtil.dart';

/// 意见输入框最大数
const int MaxNumOfFeedback = 200;

/**
 * 意见反馈父类页面
 */
class IdeaFeedbackWidget extends StatefulWidget {
  const IdeaFeedbackWidget({Key? key}) : super(key: key);

  @override
  State<IdeaFeedbackWidget> createState() => IdeaFeedbackWidgetState();
}

class IdeaFeedbackWidgetState extends State<IdeaFeedbackWidget> {
  IIdeaFeedbackUtil? util;

  TextEditingController contentController = TextEditingController();
  FocusNode contentFocusNode = FocusNode();

  String? contact;
  String? content;

  //边距
  double border = 20; //16.w;

  //是否能提交
  bool canCommit = false;

  // 图片base64地址列表
  List<String> _imagePathList = [];

  @override
  void initState() {
    super.initState();
    assert(util != null, "子类必须要initState方法super之前初始化util，或者override util");
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      sideMargin: 0,
      navBarHide: false,
      title: "意见反馈",
      body: SingleChildScrollView(
        padding: EdgeInsets.symmetric(horizontal: border.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            /// 建议的内容
            contentOfOpinionsWidget(),

            /// 上传截图
            // uploadAScreenshotWidget(),

            /// 提交
            SilButton(
              title: "提交",
              height: 40.w,
              useConstrained: true,
              backgroundColor:
                  canCommit ? ColorUtils.color_theme : ColorUtils.color_theme,
              radius: 6.w,
              titleStyle: TextStyle(
                  fontSize: 16.sp,
                  color: ColorUtils.color_font_main,
                  fontWeight: FontWeight.w500),
              onTapFunc: submitHandler,
            ),
            SizedBox(height: 28.w)
          ],
        ),
      ),
    );
  }

  /// 提交
  submitHandler() {
    util!.onCommitClick(
        contact: contact, content: content, imgUrls: _imagePathList);
  }

  /// 上传截图
  uploadAScreenshotWidget() {
    return Container(
      margin: EdgeInsets.only(bottom: 47.w),
      child: Column(
        children: [
          SilText(
            '上传问题截图',
            style: TextStyle(
                fontSize: 14.sp,
                color: ColorUtils.color_font_main,
                fontWeight: FontWeight.w500),
          ),
          SizedBox(
            height: 16.w,
          ),
          SizedBox(
            child: UpLoadPicsWidget(
              rebackResultFunc: (imgPathList) {
                _imagePathList = imgPathList;
              },
            ),
          ),
        ],
      ),
    );
  }

  /// 意见内容
  contentOfOpinionsWidget() {
    Widget inputWidget = Container(
        child: Container(
      margin: EdgeInsets.only(top: 6.w, left: 6.w, right: 6.w),
      child: SilTextInput(
        hint: '请详细描述您的优化建议，我们会仔细阅读每一条建议，选取有用的做优化调整',
        maxLength: MaxNumOfFeedback,
        onChanged: (str) {
          content = str;
          checkValid();
          setState(() {});
        },
      ),
    ));

    return Container(
        margin: EdgeInsets.only(top: 26.w, bottom: 40.w),
        child: Column(
          children: [
            Row(
              children: [
                SilText(
                  "优化建议",
                  style: TextStyle(
                      fontSize: 14.sp,
                      color: ColorUtils.color_font_main,
                      fontWeight: FontWeight.w500),
                ),
              ],
            ),
            //建议块
            Container(
              margin: EdgeInsets.only(top: 16.w),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(16.w)),
                color: ColorUtils.color_font_minor,
              ),
              height: 190.w,
              child: Column(
                children: [
                  Expanded(
                    child: inputWidget,
                  ),
                  Container(
                    alignment: Alignment.centerRight,
                    margin: EdgeInsets.only(right: 12.w),
                    child: SilText(
                      content != null
                          ? "${content!.length}/${MaxNumOfFeedback}"
                          : '0/${MaxNumOfFeedback}',
                      style: TextStyle(
                          fontSize: 14.sp, color: ColorUtils.color_150_151_153),
                    ),
                  ),
                  SizedBox(
                    height: 13.w,
                  )
                ],
              ),
            ),
          ],
        ));
  }

  /**
   * 校验是否能点击发送
   */
  checkValid() {
    if (util!.shouldContact()) {
      if (!SilTools.stringIsEmpty(content)) {
        canCommit = true;
      } else {
        canCommit = false;
      }
    } else {
      if (!SilTools.stringIsEmpty(content)) {
        canCommit = true;
      } else {
        canCommit = false;
      }
    }

    setState(() {});
  }
}

/**
 * 多行输入框
 */
class SilTextInput extends StatefulWidget {
  final String? hint;
  final double? height;
  final int maxLength;

  final Function(String)? onChanged;

  SilTextInput({
    this.hint,
    this.onChanged,
    this.height,
    this.maxLength = 200,
  });

  @override
  State createState() {
    return SilTextInputState();
  }
}

class SilTextInputState extends State<SilTextInput> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _textInput(widget.hint!),
    );
  }

  String result = '';

  _textInput(String hint) {
    return Stack(
      alignment: AlignmentDirectional.bottomEnd,
      children: [
        Container(
          alignment: AlignmentDirectional.topStart,
          child: CupertinoTextField(
            placeholder: hint,
            placeholderStyle: TextStyle(
                fontSize: 14.sp,
                color: ColorUtils.color_font_minor,
                height: 1.5),
            style: TextStyle(
                fontSize: 14.sp,
                color: ColorUtils.color_font_main,
                height: 1.5),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(2)),
            ),
            maxLines: 20,
            maxLength: widget.maxLength,
            textInputAction: TextInputAction.unspecified,
            onChanged: (str) {
              result = str;
              widget.onChanged!(result);
              setState(() {});
            },
          ),
        ),
      ],
    );
  }
}
